
<template>
  <ex_select_auto :option="option" filterable ref="main" url="/tms-api/base/TmsPlaceInfoRpc/selectLabel.json" :value="value"
    :urlParams="_urlParams" :placeholder="$tt('tip_please_select_place')" @focus="_focus" :urlControl="urlControlFun"
    v-bind="$attrs" v-on="$listeners" @initList="initList" />
</template>


<script>
export default {
  name: 'place_select',

  props: {
    value: null,
    checkCountry: {
      type: Boolean,
      default: true,
    },
    countryCode: {
      type: String,
    },
    typeEnum: {
      type: String,
    },
    tmsCountryType: {
      type: String,
      default: 'SINGLE',
    },
    option: {
      type: Object,
      default: () => {
        return { label: 'placeName', value: 'id' }
      }
    },
  },
  watch: {
    typeEnum: function (newVal, oldVal) {
      this.$nextTick(() => {
        this.$refs.main.reloadData()
      })
    },
    countryCode: function (newVal, oldVal) {
      if (this.checkCountry) {
        this.$nextTick(() => {
          const data = {}
          if(oldVal)data.initValue = null
          this.$refs.main.reloadData(data)
        })
      }
    },
  },
  methods: {
    initList(list) {
      // console.log("initList")
      // if (!list.some(it => it[this.option.value] == this.value)) {
      //   this.$emit("input", null)
      //   this.$emit("change", null)
      //   this.$emit("changeItem", null)
      // }
    },
    _urlParams() {
      const out = {}
      if (this.countryCode) out.countryCode = this.countryCode
      if (this.typeEnum && this.typeEnum != "ALL") out.typeEnum = this.typeEnum
      if (this.tmsCountryType) out.tmsCountryType = this.tmsCountryType
      return out
    },
    _focus() {
      if (this.checkCountry) {
        return this.countryCode ? null : this.$message.warning(this.$tt('tip_please_select_country'))
      }
    },
    urlControlFun(params) {
      if (!this.checkCountry) return true
      if (params.countryCode) {
        return true
      } else {
        return {
          netStop: true,
          focusInnerData: [],
        }
      }
    },
  },
}
</script>
